import { Inject, Injectable } from '@nestjs/common'
import { ResultData, SUCCESS_CODE } from 'src/common/utils/result'
import { MenuService } from '../system/menu/menu.service'
import { UserService } from '../system/user/user.service'
import { ClientInfoDto, LoginDto } from './dto'

@Injectable()
export class MainService {
  constructor(
    @Inject(UserService)
    private readonly userService: UserService,
    private readonly menuService: MenuService
  ) {}
  /**
   * @description 用户登录
   * @param { LoginDto } user 登录信息
   * @param { ClientInfoDto } clientInfo 设备信息
   */
  async login(user: LoginDto, clientInfo: ClientInfoDto) {
    const loginLog = {
      ...clientInfo,
      userName: user.username,
      status: '0',
      msg: ''
    }
    // this.loginlogService.create(loginLog)
    const loginRes = await this.userService.login(user, loginLog)
    loginLog.status = loginRes.code === SUCCESS_CODE ? '0' : '1'
    loginLog.msg = loginRes.msg
    // 用户登录日志，暂未完成
    // this.loginlogService.create(loginLog)
    return loginRes
  }

  /** 退出登录 */
  async logout(clientInfo: ClientInfoDto) {
    const loginLog = {
      ...clientInfo,
      userName: '',
      status: '0',
      msg: '退出成功'
    }
    console.log(loginLog)
    return ResultData.ok({})
  }

  /** 获取菜单信息 */
  async getRouters(userId: number) {
    const menus = await this.menuService.getMenuListByUserId(userId)
    return ResultData.ok(menus)
  }
}
